
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

class Person implements Comparable {

    int id;
    String name;

    public Person(int id, String name) {
        this.name = name;
        this.id = id;
    }

    @Override
    public String toString() {
        return "Person{" + "id=" + id + ", name=" + name + '}';
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if (this.id != other.id) {
            return false;
        }
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public int compareTo(Object o) {
        Person p=(Person)o;
        return this.id-p.id;
    }

}

/**
 * 1、定义Person类，重写equals方法， 2、泛型定义List、ArrayList、LinkedList， 参数类型为Person
 *
 * @author Administrator
 */
public class Main {

    public static void main(String[] args) {

        System.out.println("\n\n类equals方法的重写测试，与==的区别");
        Person p1 = new Person(1, "name1");
        Person p2 = new Person(1, "name1");
        if (p1 == p2) {
            System.out.println("p1==p2");
        } else {
            System.out.println("p1!=p2");
        }
        if (p1.equals(p2)) {
            System.out.println("p1.equals(p2)=true");
        } else {
            System.out.println("p1.equals(p2)=false");
        }

        System.out.println("\n\n Person类ArrayList测试");
        Person p3 = new Person(1, "name ArrayList:1");
        List<Person> l1 = new ArrayList<Person>();
        for (int i = 0; i < 10; i++) {
            l1.add(new Person(i, "name ArrayList:" + i));
        }
        for (Person p : l1) {
            System.out.println(p);
        }
        int index = Collections.binarySearch(l1, p3,null);
        if(index>=0){
            System.out.println(p3.toString()+"和ArrayList中第"+index+"个数据相同");
        }
        System.out.printf("\n洗牌后,链表中的数据\n");
        Collections.shuffle(l1);
        for (Person p : l1) {
            System.out.println(p);
        }
        int index2 = Collections.binarySearch(l1, p3,null);
        if(index2>=0){
            System.out.println("洗牌后："+p3.toString()+"和ArrayList中第"+index2+"个数据相同");
        }
        
        
        System.out.println("\n\nPerson类LinkedList测试");
        List<Person> l2 = new LinkedList<Person>();
        for (int i = 0; i < 10; i++) {
            l2.add(new Person(i, "name LinkedList:" + i));
        }
        for (Person p : l2) {
            System.out.println(p);
        }

    }
}
